home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Basic Question on SWITCH
- Date: 25 Jan 1996 20:29:09 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e8p6m$n8q@ns.RezoNet.NET>
- References: <4e4cu4$95f@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, HOTARD says...
- >I am just learning how to program in C, and I had a question about
- >switch.
- >I am writing a program that looks at a character and then determines
- >if it is a letter or number. This program must use the switch
- >command , can I place a range on the case command somehow??
- >
- >ie: Switch (var)
- > case 0-9:
- >
- >or case (isdigit):
- >
- >would anything like this work???
-
- No, the only thing you can do is to give a series of different case
- labels, like:
-
- switch (character)
- {
- case '0':
- case '1':
- case '2':
- [etc]
- case '9':
- [process a digit here]
- break;
-
- case 'a':
- case 'b':
- etc.
-
- default:
- [none of the above]
-
- A switch is not a good choice for what you're trying to do - although
- it might produce quite efficient code, the source is very verbose.
-
- If you must use switches for your exercise, you *could* say:
-
- switch (isdigit(character))
- {
- case 0:
- switch (isalpha(character))
- {
- case 0:
- [process not a digit nor an alpha]
- break;
-
- default:
- [process the alpha];
- }
- break;
-
- default:
- [process the digit]
- }
-
- but this is a pretty obscure piece of code!! Note that you can't use
- "case 1:" to get the isalpha or isdigit case, because these return zero
- or non-zero, not zero or one.
-
- [Note: your From: header line does not contain a valid email address]
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-